package com.itheima.day13work;

public class Cooker implements Runnable {
    private Desk desk;

    public Cooker(Desk desk) {
        this.desk=desk;
    }

    @Override
    public void run() {
        while (true) {
            synchronized (desk.getObj()) {
                if (desk.getCount() == 0) {
                    break;  //昨晚指定份额，直接跳出循环结束
                } else {
                    if (!desk.isFlag()) {  //桌子上没有。厨师行动
                        System.out.println("厨师正在做一份泡芙");
                        desk.setFlag(true);
                        desk.getObj().notifyAll();
                    }else { //桌上有，厨师等待
                        try {
                            desk.getObj().wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

                    }                }
            }
        }

    }
}
